package cn.bdqn.fiter;

import cn.bdqn.common.error.TokenError;
import cn.bdqn.util.JwtUtil;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.List;

public class JwtFilter extends  GenericFilterBean {
    private List<String> whiteList= Arrays.asList(
            "/login",
            "/"
    );
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        if(whiteList.contains(req.getServletPath())){
            chain.doFilter(request,response);
        }else {
            String token = req.getHeader("Authentication");
            token="aaa";
            Object body = null;
            try {
                body = JwtUtil.parseJwt(token);
            } catch (Exception e) {
                throw new ServletException("无效Token");
            }
            if(body!=null){
                chain.doFilter(request,response);
            }
        }
    }
}
